package com.ltv.xiangqiplugin.models;

import java.util.Vector;

public class Pao extends Piece { // Canon
	private static Pao pao;

	private Pao() {
	}

	public static Vector<Destination> possibleDestinations(char [][] board, SquareCoord origin, boolean checkCheck) {
		if (pao == null) {
			pao = new Pao();
		}

		return pao._possibleDestinations(board,origin, checkCheck);
	}

	public Vector<Destination> _possibleDestinations(char [][] board, SquareCoord origin, boolean checkCheck) {
		Vector<Destination> moves = new Vector<Destination>();
		int x = origin.getRow();
		int y = origin.getFile();
		char [][] tempBoard = fillTempBoard(board);
		tempBoard[x][y] = ' ';
		char piece = board[x][y];
		char color = ' ';
		if (Character.isUpperCase(piece)) color = 'r';
		else color = 'b';

		int i = 0;
		// N
		for (i = 1; x+i < 10 && board[x+i][y] == ' '; i++) {
			checkMove(moves, x+i, y, piece, color, tempBoard, board, checkCheck);
		}
		for (i++; x+i < 10 && board[x+i][y] == ' '; i++);
		if (x+i < 10) {
			checkMove(moves, x+i, y, piece, color, tempBoard, board, checkCheck);
		}

		// S
		for (i = 1; x-i > -1 && board[x-i][y] == ' '; i++) {
			checkMove(moves, x-i, y, piece, color, tempBoard, board, checkCheck);
		}
		for (i++; x-i > -1 && board[x-i][y] == ' '; i++);
		if (x-i > -1) {
			checkMove(moves, x-i, y, piece, color, tempBoard, board, checkCheck);
		}

		// E
		for (i = 1; y+i < 9 && board[x][y+i] == ' '; i++) {
			checkMove(moves, x, y+i, piece, color, tempBoard, board, checkCheck);
		}
		for (i++; y+i < 9 && board[x][y+i] == ' '; i++);
		if (y+i < 9) {
			checkMove(moves, x, y+i, piece, color, tempBoard, board, checkCheck);
		}

		// W
		for (i = 1; y-i > -1 && board[x][y-i] == ' '; i++) {
			checkMove(moves, x, y-i, piece, color, tempBoard, board, checkCheck);
		}
		for (i++; y-i > -1 && board[x][y-i] == ' '; i++);
		if (y-i > -1) {
			checkMove(moves, x, y-i, piece, color, tempBoard, board, checkCheck);
		}

		return moves;
	}
}
